<?php
include_once "UserDate.php";

class User
{
    /**
     * 用户id
     * @var int
     */
    private $id = 0;
    /**
     * 用户文件列表
     * @var array
     */
    private $files = array();
    /**
     * 用户昵称
     * @var string
     */
    private $name = "";
    /**
     * 用户头像地址
     * @var string
     */
    private $headIcon = "";
    /**
     * 用户QQ
     * @var int
     */
    private $qq = 0;
    /**
     * 用户邮箱
     * @var string
     */
    private $email = "";

    /**
     * 用户账号
     * @var string
     */
    private $uNum = "";

    /**
     * 用户密码
     * @var string
     */
    private $uPass = "";
    /**
     * 用户权限
     * @var array
     */
    private $permissions = [];
    /**
     * 用户json数据
     * @var array
     */
    private $json = [];
    /**
     * 权限过期日期
     * @var UserDate
     */
    private $userDate;

    public function __construct()
    {
        $this->userDate = new UserDate;
    }


    public function getUserDate(): UserDate
    {
        return $this->userDate;
    }

    public function setUserTargetTime(UserDate $userData)
    {
        $this->permissions["time"] = $userData->getTarGetTime();
    }

    public function write($arr)
    {
        $this->qq = $arr["qq"];
        $this->name = $arr["name"];
        $this->email = $arr["email"];
        $this->uNum = $arr["uNum"];
        $this->uPass = $arr["uPass"];
        $this->permissions = json_decode($arr["permissions"], true);
        $this->headIcon = $arr["head_icon"];
    }

    public function parse($arr)
    {
        $this->permissions = $arr["permissions"];
        $this->name = $arr["name"];
        $this->qq = $arr["qq"];
        $this->uNum = $arr["uNum"];
        $this->uPass = $arr["uPass"];
        $this->email = $arr["email"];
        $this->headIcon = $arr["head_icon"];
        $this->userDate = new UserDate($this->permissions["time"]);
    }

    /**
     * @return mixed
     */
    public function getJson()
    {
        $thisJson = $this->json;
        $thisJson["id"] = $this->id;
        $thisJson["name"] = $this->name;
        $thisJson["head_icon"] = $this->headIcon;
        $thisJson["qq"] = $this->qq;
        $thisJson["user"] = $this->uNum;
        $thisJson["pass"] = $this->uPass;
        $thisJson["email"] = $this->email;
        $thisJson["permissions"] = json_encode($this->permissions);
        $thisJson["files"] = json_encode($this->files);
        return json_encode($thisJson, true);
    }

    public function addFile($fileName)
    {
        $this->files[] = $fileName;
    }

    public function removeFile($fId)
    {
        unset($this->files[$fId]);
    }

    public function getFiles()
    {
        return $this->files;
    }

    /**
     * @return string
     */
    public function getUNum(): string
    {
        return $this->uNum;
    }

    /**
     * @param string $uNum
     */
    public function setUNum(string $uNum)
    {
        $this->uNum = $uNum;
    }

    /**
     * @return string
     */
    public function getUPass(): string
    {
        return $this->uPass;
    }

    /**
     * @param string $uPass
     */
    public function setUPass(string $uPass)
    {
        $this->uPass = $uPass;
    }


    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     * @return User
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getHeadIcon()
    {
        return $this->headIcon;
    }

    /**
     * @param mixed $headIcon
     * @return User
     */
    public function setHeadIcon($headIcon)
    {
        $this->headIcon = $headIcon;
        return $this;
    }

    /**
     * @return int
     */
    public function getQq()
    {
        return $this->qq;
    }

    /**
     * @param int $qq
     * @return User
     */
    public function setQq($qq)
    {
        $this->qq = $qq;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
     * @return bool
     */
    public function isMember()
    {
        return $this->permissions["type"] == UserManager::$MEMBER;
    }

    /**
     * @return User
     */
    public function setMember()
    {
        $this->permissions["type"] = UserManager::$MEMBER;
        return $this;
    }

    /**
     * @return bool
     */
    public function isUniversal()
    {
        return $this->permissions["type"] == UserManager::$UNIVERSAL;
    }

    /**
     * @return User
     */
    public function setUniversal()
    {
        $this->permissions["type"] = UserManager::$UNIVERSAL;
        return $this;
    }

    /**
     * @return bool
     */
    public function isAdmin()
    {
        return $this->permissions["type"] == UserManager::$ADMIN;
    }

    /**
     * @return User
     */
    public function setAdmin()
    {
        $this->permissions["type"] = UserManager::$ADMIN;
        return $this;
    }
}